package com.greenantmall.activity;

import java.io.Serializable;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVUser;
import com.greenantmall.AntHomeApplication;
import com.greenantmall.R;
import com.greenantmall.bean.ServerSection;
import com.greenantmall.bean.ShippingAddr;
import com.greenantmall.bean.ShoppingCart;
import com.greenantmall.bean.StoreInfo;
import com.greenantmall.service.RetrieveDataAsyn;
import com.greenantmall.service.RetrieveDataListener;
import com.greenantmall.utils.Const;
import com.greenantmall.utils.PayCompleteListener;
import com.greenantmall.utils.UtilTools;
import com.greenantmall.view.ChoosePayMethodDialog;
import com.lsarah.utils.SystemBarTintManager;

public class OrderCreateActivity extends BaseActivity {
	private View mBackView;
	private Button mBtnModify;
	private Button mBtnCreateOrder;
	private EditText mEtCustomRemark;
	private LinearLayout mOrderInfoItemContainer;
	private TextView mTvReceiver;
	private TextView mTvPhone;
	private TextView mTvAddress;
	private TextView mTvSendTime;
	private TextView mTvZitidian;
	private TextView mTvTotalMoney;
	private CheckBox mCbPayMethodOnline;
	private CheckBox mCbPayMethodDelivery;
	private CheckBox mCbZiti;
	private CheckBox mCbShangmen;
	
	public static final String KeySendTime = "key_send_time";
	public static final String KeyGoodsList = "key_goods_list";
	
	private DecimalFormat mDecimalFormat;
	private ServerSection mServerSection;
	private boolean isCommitting = false;
	private boolean isPriceByWeight = false;
	private Date mSendDate;
	private List<ShoppingCart> mShoppingCartList = new ArrayList<ShoppingCart>();
	private ShippingAddr mShippingAddr;
	private List<String> mOrderStoreIdList = new ArrayList<String>();
	private List<OrderInfoItemHolder> mItemHolderList = new ArrayList<OrderInfoItemHolder>();
	private List<OrderInfoItemHolder> mTempItemHolderList = new ArrayList<OrderInfoItemHolder>();
	private List<OrderInfoItemHolder> mTempItemHolderListFailed = new ArrayList<OrderInfoItemHolder>();
	private OrderInfoItemHolder mTempOrderInfoItemHolder;
	
	private OnClickListener mOnClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			if (v.equals(mBackView)) {
				OrderCreateActivity.this.finish();
			} else if (v.equals(mBtnModify)) {
				Intent intent = new Intent(OrderCreateActivity.this,
						MyAddressActivity.class);
				intent.putExtra(MyAddressActivity.KEY_MODE,
						MyAddressActivity.MODE_CHOOSE);
				startActivityForResult(intent, 0);
			} else if (v.equals(mBtnCreateOrder)) {
				// 检查收货人信息
				if (mShippingAddr == null
						|| TextUtils.isEmpty(mShippingAddr.getName())
						|| TextUtils.isEmpty(mShippingAddr.getMobile())
						|| TextUtils.isEmpty(mShippingAddr.getUptown())) {
					UtilTools.showMyToast(
							OrderCreateActivity.this,
							getResources().getString(
									R.string.order_ensure_receiver_info_error));
					return;
				}

				if (isCommitting) {
					return;
				}
				isCommitting = true;

				AlertDialog dialog = new AlertDialog.Builder(
						OrderCreateActivity.this)
						.setTitle(R.string.order_ensure_order_commit_prompt)
						.setMessage(
								R.string.order_ensure_order_commit_ensure_prompt)
						.setNegativeButton(R.string.sure,
								new DialogInterface.OnClickListener() {
									@Override
									public void onClick(DialogInterface dialog,
											int which) {
										commitOrder();
									}
								})
						.setPositiveButton(R.string.cancel,
								new DialogInterface.OnClickListener() {
									@Override
									public void onClick(DialogInterface dialog,
											int which) {
										isCommitting = false;
									}
								}).create();
				dialog.setCanceledOnTouchOutside(false);
				dialog.setCancelable(false);
				dialog.show();
			}
		}
	};

	private OnCheckedChangeListener mOnCheckedChangeListener = new OnCheckedChangeListener() {
		@Override
		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			if (buttonView.equals(mCbShangmen)) {
				if (isChecked) {
					mCbShangmen.setChecked(true);
					mCbShangmen.setEnabled(false);
					mCbZiti.setChecked(false);
					mCbZiti.setEnabled(true);
					
					for(OrderInfoItemHolder itemHolder : mItemHolderList) {
						itemHolder.tvTransportMoney.setText(mDecimalFormat.format(itemHolder.freight));
						if (!isPriceByWeight) {
							itemHolder.tvGoodsMoney.setText(mDecimalFormat.format(itemHolder.goodsMoney));
							itemHolder.shouldPayMoney = itemHolder.shouldPayMoney + itemHolder.freight;
							itemHolder.tvShouldPayMoney.setText(mDecimalFormat.format(itemHolder.shouldPayMoney));
						}
					}
					refreshTotalMoney();
				}
			} else if (buttonView.equals(mCbZiti)) {
				if (isChecked) {
					mCbZiti.setChecked(true);
					mCbZiti.setEnabled(false);
					mCbShangmen.setChecked(false);
					mCbShangmen.setEnabled(true);

					for(OrderInfoItemHolder itemHolder : mItemHolderList) {
						itemHolder.tvTransportMoney.setText(mDecimalFormat.format(0));
						if (!isPriceByWeight) {
							itemHolder.tvGoodsMoney.setText(mDecimalFormat.format(itemHolder.goodsMoney));
							itemHolder.shouldPayMoney = itemHolder.shouldPayMoney - itemHolder.freight;
							itemHolder.tvShouldPayMoney.setText(mDecimalFormat.format(itemHolder.shouldPayMoney));
							mTvTotalMoney.setText(mDecimalFormat.format(itemHolder.shouldPayMoney));
						}
					}
					refreshTotalMoney();
				}
			} else if (buttonView.equals(mCbPayMethodOnline)) {
				if (isChecked) {
					mCbPayMethodOnline.setChecked(true);
					mCbPayMethodOnline.setEnabled(false);
					mCbPayMethodDelivery.setChecked(false);
					mCbPayMethodDelivery.setEnabled(true);
				}
			} else if (buttonView.equals(mCbPayMethodDelivery)) {
				if (isChecked) {
					mCbPayMethodDelivery.setChecked(true);
					mCbPayMethodDelivery.setEnabled(false);
					mCbPayMethodOnline.setChecked(false);
					mCbPayMethodOnline.setEnabled(true);
				}
			}
		}
	};

	private RetrieveDataListener mRetrieveDataListenerForAddress = new RetrieveDataListener() {
		@Override
		public void dataRetrieved(int requestId, Object data) {
			Log.d("CEXX", "CEXX-mRetrieveDataListenerForAddress-data : " + data);
			// 查询地址
			if (data != null) {
				List<ShippingAddr> list = (List<ShippingAddr>) data;
				Log.d("CEXX", "CEXX-mRetrieveDataListenerForAddress-list.size : " + list.size());
				if (list.size() > 0) {
					mShippingAddr = list.get(0);
					String name = mShippingAddr.getName();
					String mobile = mShippingAddr.getMobile();
					String uptown = mShippingAddr.getUptown();
					String door = mShippingAddr.getDoor();

					name = (name == null ? "" : name);
					mobile = (mobile == null ? "" : mobile);
					uptown = (uptown == null ? "" : uptown);
					door = (door == null ? "" : door);

					mTvReceiver.setText(name);
					mTvPhone.setText(mobile);
					mTvAddress.setText(uptown + door);
				}
			}
		}
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		SystemBarTintManager.setContentView(this, R.layout.order_create,
				getResources().getColor(R.color.green));
		mBackView = findViewById(R.id.order_create_top_arrow_left);
		mBtnModify = (Button) findViewById(R.id.order_create_btn_modify);
		mBtnCreateOrder = (Button) findViewById(R.id.order_create_btn);
		mOrderInfoItemContainer = (LinearLayout) findViewById(R.id.order_create_orderinfo_container);
		mEtCustomRemark = (EditText) findViewById(R.id.order_create_et_custom_remark);
		mTvReceiver = (TextView) findViewById(R.id.order_create_tv_receiver);
		mTvPhone = (TextView) findViewById(R.id.order_create_tv_phone);
		mTvAddress = (TextView) findViewById(R.id.order_create_tv_address);
		mTvSendTime = (TextView) findViewById(R.id.order_create_tv_time);
		mTvZitidian = (TextView) findViewById(R.id.order_create_tv_ziti_dian);
		mTvTotalMoney = (TextView) findViewById(R.id.order_create_total_money);
		mCbPayMethodOnline = (CheckBox) findViewById(R.id.order_create_send_method_check_pay_online);
		mCbPayMethodDelivery = (CheckBox) findViewById(R.id.order_create_send_method_check_cash_on_delivery);
		mCbZiti = (CheckBox) findViewById(R.id.order_create_send_method_check_ziti);
		mCbShangmen = (CheckBox) findViewById(R.id.order_create_send_method_check_shangmen);

		mBackView.setOnClickListener(mOnClickListener);
		mBtnModify.setOnClickListener(mOnClickListener);
		mBtnCreateOrder.setOnClickListener(mOnClickListener);
		mCbPayMethodOnline.setOnCheckedChangeListener(mOnCheckedChangeListener);
		mCbPayMethodDelivery
				.setOnCheckedChangeListener(mOnCheckedChangeListener);
		mCbZiti.setOnCheckedChangeListener(mOnCheckedChangeListener);
		mCbShangmen.setOnCheckedChangeListener(mOnCheckedChangeListener);

		mDecimalFormat = new DecimalFormat("#########.##");

		// pickup station
		mServerSection = UtilTools.getServerSection(getApplication());
		String pickupStation = mServerSection.getPickupStation();
		mTvZitidian.setText(TextUtils.isEmpty(pickupStation) ? ""
				: pickupStation);

		// read address
		RetrieveDataAsyn.getInstance()
				.getAddressList(AVUser.getCurrentUser().getObjectId(), -1,
						mRetrieveDataListenerForAddress);

		// read data from intent
		readDataFromIntent();
		// handle order info item layout
		handleOrderInfoItemLayout();
	}

	private void readDataFromIntent() {
		mCbPayMethodOnline.setChecked(true);
		mCbPayMethodOnline.setEnabled(false);
		mCbPayMethodDelivery.setChecked(false);
		mCbPayMethodDelivery.setEnabled(true);
		mCbZiti.setChecked(false);
		mCbZiti.setEnabled(true);
		mCbShangmen.setChecked(true);
		mCbShangmen.setEnabled(false);
		
		Intent intent = getIntent();
		Serializable dateSerializable = intent.getSerializableExtra(KeySendTime);
		if (dateSerializable != null) {
			mSendDate = (Date) dateSerializable;
			mTvSendTime.setText(DateFormat.format("yy-MM-dd HH:mm", mSendDate));
		}

		boolean haveGoodsList = intent.getBooleanExtra(KeyGoodsList, false);
		List<ShoppingCart> goodsList = (haveGoodsList ? Const.mShoppingCartList : null);
		Const.mShoppingCartList = null;
		if (goodsList != null) {
			mShoppingCartList.addAll(goodsList);
		}
		Log.d("CEXX", "CEXX-OrderCreateActivity-intent-mGoodsList.size:" + mShoppingCartList.size());
		
		for(ShoppingCart shoppingCart : mShoppingCartList) {
			String scStoreId = shoppingCart.getStoreId();
			if (!TextUtils.isEmpty(scStoreId)) {
				if(!mOrderStoreIdList.contains(scStoreId)) {
					mOrderStoreIdList.add(scStoreId);
				}
			}
		}
		Log.d("CEXX", "CEXX-OrderCreateActivity-intent-mOrderStoreIdList.size:" + mOrderStoreIdList.size());
	}
	
	private void handleOrderInfoItemLayout() {
		HashMap<String, StoreInfo> storeInfosMap = AntHomeApplication.storeInfos;
		if(storeInfosMap != null) {
			for(String storeId : mOrderStoreIdList) {
				StoreInfo storeInfo = storeInfosMap.get(storeId);
				if(storeInfo != null) {
					List<ShoppingCart> tempList = new ArrayList<ShoppingCart>();
					for(ShoppingCart sc : mShoppingCartList) {
						if(storeId.equals(sc.getStoreId())) {
							tempList.add(sc);
						}
					}
					
					OrderInfoItemHolder itemHolder = new OrderInfoItemHolder();
					itemHolder.setStoreInfo(storeInfo, tempList);
					mItemHolderList.add(itemHolder);
					mOrderInfoItemContainer.addView(itemHolder.contentView, 
							LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
				}
			}
		}
		
		refreshTotalMoney();
	}
	
	private void refreshTotalMoney() {
		float totalMoney = 0;
		for(OrderInfoItemHolder itemHolder : mItemHolderList) {
			totalMoney += itemHolder.shouldPayMoney;
		}
		mTvTotalMoney.setText(mDecimalFormat.format(totalMoney));
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		Log.d("CEXX", "CEXX-OrderCreateActivity-requestCode : "
				+ requestCode);
		Log.d("CEXX", "CEXX-OrderCreateActivity-resultCode : "
				+ resultCode);
		if (data != null) {
			String name = data
					.getStringExtra(MyAddressEditActivity.KEY_ADDRESS_NAME);
			String mobile = data
					.getStringExtra(MyAddressEditActivity.KEY_ADDRESS_MOBILE);
			String uptown = data
					.getStringExtra(MyAddressEditActivity.KEY_ADDRESS_UPTOWN);
			String door = data
					.getStringExtra(MyAddressEditActivity.KEY_ADDRESS_DOOR);

			name = (name == null ? "" : name);
			mobile = (mobile == null ? "" : mobile);
			uptown = (uptown == null ? "" : uptown);
			door = (door == null ? "" : door);

			mTvReceiver.setText(name);
			mTvPhone.setText(mobile);
			mTvAddress.setText(uptown + door);

			mShippingAddr = new ShippingAddr();
			mShippingAddr.setUserId(AVUser.getCurrentUser().getObjectId());
			mShippingAddr.setDoor(door);
			mShippingAddr.setMobile(mobile);
			mShippingAddr.setName(name);
			mShippingAddr.setUptown(uptown);
		}
	}

	private void commitOrder() {
		if (mItemHolderList.size() == 0) {
			isCommitting = false;
			return;
		}
		
		mTempItemHolderListFailed.clear();
		mTempItemHolderList.clear();
		mTempItemHolderList.addAll(mItemHolderList);
		mTempOrderInfoItemHolder = null;
		this.commitSingleOrder(mTempItemHolderList.remove(0));
	}
	
	private void commitSingleOrder(OrderInfoItemHolder itemHolder) {
		mTempOrderInfoItemHolder = itemHolder;
		
		boolean isSpOrder = (StoreInfo.STORE_TYPE_SP.equals(itemHolder.storeInfo.getStoreStyle()));
		Log.d("CEXX", "CEXX-OrderCreateActivity-commitOrder-isSpOrder : " + isSpOrder);

		mServerSection = UtilTools.getServerSection(getApplication());
		String pickupStation = mServerSection.getPickupStation();
		pickupStation = (TextUtils.isEmpty(pickupStation) ? "" : pickupStation);
		Log.d("CEXX", "CEXX-OrderCreateActivity-commitOrder-pickupStation : " + pickupStation);

		double deliveryCost = 0;
		if (mCbShangmen.isChecked()) {
			deliveryCost = itemHolder.freight;
		}
		Log.d("CEXX", "CEXX-OrderCreateActivity-commitOrder-deliveryCost : " + deliveryCost);

		String sendMethod = getResources().getString(R.string.order_ensure_send_method_songhuoshangmen);
		if (mCbZiti.isChecked()) {
			sendMethod = getResources().getString(R.string.order_ensure_send_metho_ziting);
		}
		Log.d("CEXX", "CEXX-commitOrder-sendMethod : " + sendMethod);
		
		String payMethod = getResources().getString(R.string.order_ensure_pay_method_pay_online);
		if (mCbPayMethodDelivery.isChecked()) {
			payMethod = getResources().getString(R.string.order_ensure_pay_method_cash_on_delivery);
		}
		Log.d("CEXX", "CEXX-commitOrder-payMethod : " + payMethod);

		RetrieveDataAsyn.getInstance().makeOrder(
				AVUser.getCurrentUser().getObjectId(), itemHolder.shoppingCardList,
				mShippingAddr, mEtCustomRemark.getText().toString(),
				(mSendDate == null ? new Date() : mSendDate), payMethod,
				sendMethod, deliveryCost, isSpOrder, pickupStation,
				-1, mRetrieveDataListenerForOrder);
	}
	
	private void orderCreateFinished() {
		isCommitting = false;
		
		int sizeFailed = mTempItemHolderListFailed.size();
		int sizeTotal = mItemHolderList.size();
		if(sizeFailed == 0) {
			Log.d("CEXX", "CEXX-OrderCreateActivity-all orders commit successful.");
		} else if (sizeFailed == sizeTotal) {
			Log.d("CEXX", "CEXX-OrderCreateActivity-commit order failed.");
			UtilTools.showMyToast(OrderCreateActivity.this,
							getResources().getString(R.string.order_ensure_order_commit_failed));
		} else {
			Log.d("CEXX", "CEXX-OrderCreateActivity-part orders commit successful.");
			UtilTools.showMyToast(OrderCreateActivity.this,
							getResources().getString(R.string.order_ensure_order_commit_part_failed));
		}
		
		Intent intent = new Intent(OrderCreateActivity.this, MyOrdersActivity.class);
		startActivity(intent);
		OrderCreateActivity.this.finish();
	}
	
	private RetrieveDataListener mRetrieveDataListenerForOrder = new RetrieveDataListener() {
		@Override
		public void dataRetrieved(int requestId, Object data) {
			Log.d("CEXX", "CEXX-mRetrieveDataListenerForOrder-data : " + data);
			if(data == null) {
				// 创建订单失败
				mTempItemHolderListFailed.add(mTempOrderInfoItemHolder);
				
				if(mTempItemHolderList.size() > 0) {
					mTempOrderInfoItemHolder = null;
					commitSingleOrder(mTempItemHolderList.remove(0));
				} else {
					orderCreateFinished();
				}
			} else {
				// 创建订单成功
				if (mTempOrderInfoItemHolder.shoppingCardList != null) {
					// 从购物车中删除
					for (ShoppingCart shoppingCart : mTempOrderInfoItemHolder.shoppingCardList) {
						RetrieveDataAsyn.getInstance().removeShoppingCart(shoppingCart, -1,
								new RetrieveDataListener() {
							@Override
							public void dataRetrieved(int requestId, Object data) {
								if (data != null) {
									sendBroadcast(new Intent(Const.SHOPPING_CART_CHANGED));
								}
							}
						});
					}
				}
				
				int sizeTotal = mItemHolderList.size();
				int sizeLeft = mTempItemHolderList.size();
				if(sizeLeft == sizeTotal - 1) {
					// 更新首单优惠的状态
					AVUser avUser = AVUser.getCurrentUser();
					if(mTempOrderInfoItemHolder.supportFirstOrderDiscount
							&& mTempOrderInfoItemHolder.goodsMoney > mTempOrderInfoItemHolder.firstOrderLimit
							&& avUser != null
							&& !avUser.getBoolean("firstOrderDiscountUsed")) {
						avUser.put("firstOrderDiscountUsed", true);
						avUser.refreshInBackground(null);
					}
				}
				
				AVObject avObject = (AVObject) data;
				if (mCbPayMethodOnline.isChecked()) {
					List<AVObject> createdOrdersList = new ArrayList<AVObject>();
					createdOrdersList.add(avObject);
					new ChoosePayMethodDialog(OrderCreateActivity.this,
							createdOrdersList, mPayCompletListener)
							.show();
				} else {
					if(mTempItemHolderList.size() > 0) {
						mTempOrderInfoItemHolder = null;
						commitSingleOrder(mTempItemHolderList.remove(0));
					} else {
						orderCreateFinished();
					}
				}
			}
		}
	};

	private PayCompleteListener mPayCompletListener = new PayCompleteListener() {
		@Override
		public void payComplete() {
			if(mTempItemHolderList.size() > 0) {
				mTempOrderInfoItemHolder = null;
				commitSingleOrder(mTempItemHolderList.remove(0));
			} else {
				orderCreateFinished();
			}
		}

		@Override
		public void notChoosePayMethod() {
			if(mTempItemHolderList.size() > 0) {
				mTempOrderInfoItemHolder = null;
				commitSingleOrder(mTempItemHolderList.remove(0));
			} else {
				orderCreateFinished();
			}
		}
	};

	private class OrderInfoItemHolder {
		View contentView;
		TextView tvGoodsMoney;
		TextView tvTransportMoney;
		View viewYouhuiLayout;
		TextView tvYouhuiMoney;
		TextView tvStoreDes;
		TextView tvYouhuiPrompt;
		TextView tvShouldPayMoney;
		
		boolean supportFirstOrderDiscount;
		double firstOrderLimit;
		double firstOrderDiscount;
		
		double goodsMoney = 0;
		double freight = 0;
		double shouldPayMoney = 0;
		
		StoreInfo storeInfo;
		List<ShoppingCart> shoppingCardList;

		public OrderInfoItemHolder() {
			contentView = LayoutInflater.from(OrderCreateActivity.this)
					.inflate(R.layout.order_create_item, null);
			tvGoodsMoney = (TextView) contentView
					.findViewById(R.id.order_create_goods_money);
			tvTransportMoney = (TextView) contentView
					.findViewById(R.id.order_create_tv_transport_money);
			viewYouhuiLayout = contentView
					.findViewById(R.id.order_create_tv_youhui_layout);
			tvYouhuiMoney = (TextView) contentView
					.findViewById(R.id.order_create_tv_youhui_money);
			tvStoreDes = (TextView) contentView
					.findViewById(R.id.order_create_store_des);
			tvYouhuiPrompt = (TextView) contentView
					.findViewById(R.id.order_create_youhui_prompt);
			tvShouldPayMoney = (TextView) contentView
					.findViewById(R.id.order_create_should_pay_money);
		}
		
		public void setStoreInfo(StoreInfo storeInfo, List<ShoppingCart> shoppingCartList) {
			this.storeInfo = storeInfo;
			this.shoppingCardList = shoppingCartList;
			
			freight = this.calculateFreight();
			
			if (mCbShangmen.isChecked()) {
				tvTransportMoney.setText(mDecimalFormat.format(freight));
			}

			for (ShoppingCart usc : shoppingCartList) {
				goodsMoney += usc.getCost();
				if (usc.getGoods().isWeighAble()) {
					isPriceByWeight = true;
					break;
				}
			}
			
			String storeDes = storeInfo.getDescrip();
			tvStoreDes.setText((storeDes==null ? "" : storeDes));
			handleYouhui();
			
			if (isPriceByWeight) {
				tvGoodsMoney.setText(String.valueOf("--.--"));
				tvShouldPayMoney.setText(String.valueOf("--.--"));
			} else {
				tvGoodsMoney.setText(mDecimalFormat.format(goodsMoney));
				if(goodsMoney >= firstOrderLimit) {
					shouldPayMoney = goodsMoney + freight - firstOrderDiscount;
				} else {
					shouldPayMoney = goodsMoney + freight;
				}
				tvShouldPayMoney.setText(mDecimalFormat.format(shouldPayMoney));
			}
		}
		
		private void handleYouhui() {
			AVUser avUser = AVUser.getCurrentUser();
			if (avUser == null) {
				Log.d("CEXX", "CEXX-OrderCreateActivity-handleYouhui-00");
				return;
			}

			boolean firstOrderDiscountUsed = avUser.getBoolean("firstOrderDiscountUsed");
			Log.d("CEXX", "CEXX-OrderCreateActivity-handleYouhui-firstOrderDiscountUsed:" + firstOrderDiscountUsed);
			if (firstOrderDiscountUsed) {
				return;
			}

			supportFirstOrderDiscount = storeInfo.isHaveDisaccount();
			firstOrderLimit = storeInfo.getFirstOrderLimit();
			firstOrderDiscount = storeInfo.getFirstOrderDiscount();
			Log.d("CEXX", "CEXX-OrderCreateActivity-mSupportFirstOrderDiscount:" + supportFirstOrderDiscount);
			Log.d("CEXX", "CEXX-OrderCreateActivity-mFirstOrderLimit:" + firstOrderLimit);
			Log.d("CEXX", "CEXX-OrderCreateActivity-mFirstOrderDiscount:" + firstOrderDiscount);

			if (supportFirstOrderDiscount && !firstOrderDiscountUsed) {
				tvYouhuiPrompt.setVisibility(View.VISIBLE);
				String format = getResources().getString(R.string.order_ensure_youhui_prompt);
				tvYouhuiPrompt.setText(String.format(format, firstOrderLimit, firstOrderDiscount));

				viewYouhuiLayout.setVisibility(View.VISIBLE);
				if(goodsMoney >= firstOrderLimit) {
					tvYouhuiMoney.setText(String.valueOf(-1 * firstOrderDiscount));
				}
				else {
					tvYouhuiMoney.setText(String.valueOf(0));
				}
			}
		}
		
		private float calculateFreight() {
			float freight = 0;

			if (storeInfo != null) {
				Log.d("CEXX", "CEXX-OrderCreateActivity-storeInfo.getDescrip : " + storeInfo.getDescrip());
				
				double startCost = storeInfo.getStartcost();
				double deliveryCost = storeInfo.getDeliverycost();
				Log.d("CEXX", "CEXX-OrderCreateActivity-startCost : " + startCost);
				Log.d("CEXX", "CEXX-OrderCreateActivity-deliveryCost : " + deliveryCost);

				double tempCost = 0;
				for (ShoppingCart sc : shoppingCardList) {
					tempCost += sc.getCost();
				}

				Log.d("CEXX", "CEXX-OrderCreateActivity-tempCost : " + tempCost);
				if (tempCost < startCost) {
					freight += deliveryCost;
				}
			}

			Log.d("CEXX", "CEXX-OrderCreateActivity-freight : " + freight);
			return freight;
		}
	}
}












